/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.gatech.gellinservlet.presenter;

import edu.gatech.gellinservlet.model.*;
import javax.servlet.RequestDispatcher;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Hung
 */
public class makeTeam extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            
            //String labName = request.getParameter("labName2");
              
            HttpSession session = request.getSession(true);
          //  LabManager labData = (LabManager) getServletContext().getAttribute("labData");
            //get session Lab object
            Lab aLab = (Lab)session.getAttribute("persistLab");
            //out.println("labName " + aLab.getName());
            if(aLab != null){
           //Lab aLab = labData.session.getAsearch(labName);
                //get the max team size and team designation from lab and create a new team
                //with the team name
            
                int maxTeamSize = -1;
                int teamDesignation = -1; 
                maxTeamSize = aLab.getTeamMaxSize();
                teamDesignation = aLab.getTeamDesignation();

                String teamName = request.getParameter("teamName");
                TeamManager teamData = (TeamManager) getServletContext().getAttribute("teamData");
                LabManager labData = (LabManager) getServletContext().getAttribute("labData");
                //System.out.println(labData.toString());
                Team aTeam = labData.addTeamToLab(teamName, aLab.getID(), teamData);
                Student aUser = (Student)session.getAttribute("persistUser"); 
                //Team aTeam = teamData.createTeam(teamName, maxTeamSize, teamDesignation);
                //aLab.addTeam(aTeam);
                aTeam.add(aUser);
                aUser.addTeam(aTeam);
                for(Team teams : aLab.getTeamList())
                {
                    System.out.println(teams.getName());
                }
                getServletContext().setAttribute("teamData", teamData);
                getServletContext().setAttribute("labData", labData);
                teamData.save("teamData.ser");
                labData.save("labData.ser");
                response.sendRedirect("stude_home.jsp?msg=valid");
                
                out.println("maxTeamSize: " + maxTeamSize + "teamDesignation: " + teamDesignation + "teamName: " + teamName );
                
            }
            else{
                out.println("Failed");
            }
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet makeTeam</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet makeTeam at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
             */
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
